home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Freeware / First Page 2006 3.00 / fp2006-final-3.00-setup.exe / {app} / Iscripts / Forms Misc / cookie-forn-saver.izs < prev    next >
Text File  |  2005-09-28  |  11KB  |  639 lines

  1. <!NOWIZARD>
  2.  
  3. <!TITLE>Cookie Form Saver 
  4. <!/TITLE>
  5.  
  6. <!DESCRIPTION>This script saves the information typed into the form so that the next times the viewer visits the page the form is already filled out with their data!<!/DESCRIPTION> 
  7.  
  8. <!CATEGORY>Forms<!/CATEGORY>
  9.  
  10. <!SCRIPT>
  11. <!-- START OF SCRIPT -->
  12.  
  13.  
  14. <!-- HOW TO INSTALL COOKIE FORM SAVER:
  15.  
  16.  
  17.  
  18.   1.  Copy code into the HEAD section of document
  19.  
  20.   2.  Add the onLoad event handler into the BODY tag
  21.  
  22.   3.  Put last coding into the BODY section of document  -->
  23.  
  24.  
  25.  
  26. <!-- STEP ONE: Add code into HEAD section of document  -->
  27.  
  28.  
  29.  
  30. <HEAD>
  31.  
  32.  
  33.  
  34. <SCRIPT LANGUAGE="JavaScript">
  35.  
  36.  
  37. <!-- Original:  Nick Baker -->
  38.  
  39. <!-- Begin
  40.  
  41. // Cookie Functions  ////////////////////  (:)
  42.  
  43.  
  44.  
  45. // Set the cookie.
  46.  
  47. // SetCookie('your_cookie_name', 'your_cookie_value', exp);
  48.  
  49.  
  50.  
  51. // Get the cookie.
  52.  
  53. // var someVariable = GetCookie('your_cookie_name');
  54.  
  55.  
  56.  
  57. var expDays = 100;
  58.  
  59. var exp = new Date(); 
  60.  
  61. exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
  62.  
  63.  
  64.  
  65. function getCookieVal (offset) {  
  66.  
  67.     var endstr = document.cookie.indexOf (";", offset);  
  68.  
  69.     if (endstr == -1) { endstr = document.cookie.length; }
  70.  
  71.     return unescape(document.cookie.substring(offset, endstr));
  72.  
  73. }
  74.  
  75.  
  76.  
  77. function GetCookie (name) {  
  78.  
  79.     var arg = name + "=";  
  80.  
  81.     var alen = arg.length;  
  82.  
  83.     var clen = document.cookie.length;  
  84.  
  85.     var i = 0;  
  86.  
  87.     while (i < clen) {    
  88.  
  89.         var j = i + alen;    
  90.  
  91.         if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
  92.  
  93.         i = document.cookie.indexOf(" ", i) + 1;    
  94.  
  95.         if (i == 0) break;   
  96.  
  97.     }  
  98.  
  99.     return null;
  100.  
  101. }
  102.  
  103.  
  104.  
  105. function SetCookie (name, value) {  
  106.  
  107.     var argv = SetCookie.arguments;  
  108.  
  109.     var argc = SetCookie.arguments.length;  
  110.  
  111.     var expires = (argc > 2) ? argv[2] : null;  
  112.  
  113.     var path = (argc > 3) ? argv[3] : null;  
  114.  
  115.     var domain = (argc > 4) ? argv[4] : null;  
  116.  
  117.     var secure = (argc > 5) ? argv[5] : false;  
  118.  
  119.     document.cookie = name + "=" + escape (value) + 
  120.  
  121.     ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
  122.  
  123.     ((path == null) ? "" : ("; path=" + path)) +  
  124.  
  125.     ((domain == null) ? "" : ("; domain=" + domain)) +    
  126.  
  127.     ((secure == true) ? "; secure" : "");
  128.  
  129. }
  130.  
  131.  
  132.  
  133. // cookieForms saves form content of a page.
  134.  
  135.  
  136.  
  137. // use the following code to call it:
  138.  
  139. //  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">
  140.  
  141.  
  142.  
  143. // It works on text fields and dropdowns in IE 5+
  144.  
  145. // It only works on text fields in Netscape 4.5
  146.  
  147.  
  148.  
  149.  
  150.  
  151. function cookieForms() {  
  152.  
  153.     var mode = cookieForms.arguments[0];
  154.  
  155.     
  156.  
  157.     for(f=1; f<cookieForms.arguments.length; f++) {
  158.  
  159.         formName = cookieForms.arguments[f];
  160.  
  161.         
  162.  
  163.         if(mode == 'open') {    
  164.  
  165.             cookieValue = GetCookie('saved_'+formName);
  166.  
  167.             if(cookieValue != null) {
  168.  
  169.                 var cookieArray = cookieValue.split('#cf#');
  170.  
  171.                 
  172.  
  173.                 if(cookieArray.length == document[formName].elements.length) {
  174.  
  175.                     for(i=0; i<document[formName].elements.length; i++) {
  176.  
  177.                     
  178.  
  179.                         if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
  180.  
  181.                         else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
  182.  
  183.                         else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
  184.  
  185.                         else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
  186.  
  187.                     }
  188.  
  189.                 }
  190.  
  191.             }
  192.  
  193.         }
  194.  
  195.                 
  196.  
  197.         if(mode == 'save') {    
  198.  
  199.             cookieValue = '';
  200.  
  201.             for(i=0; i<document[formName].elements.length; i++) {
  202.  
  203.                 fieldType = document[formName].elements[i].type;
  204.  
  205.                 
  206.  
  207.                 if(fieldType == 'password') { passValue = ''; }
  208.  
  209.                 else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
  210.  
  211.                 else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
  212.  
  213.                 else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
  214.  
  215.                 else { passValue = document[formName].elements[i].value; }
  216.  
  217.             
  218.  
  219.                 cookieValue = cookieValue + passValue + '#cf#';
  220.  
  221.             }
  222.  
  223.             cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
  224.  
  225.             
  226.  
  227.             SetCookie('saved_'+formName, cookieValue, exp);        
  228.  
  229.         }    
  230.  
  231.     }
  232.  
  233. }
  234.  
  235. //  End -->
  236.  
  237. </script>
  238.  
  239.  
  240.  
  241. </HEAD>
  242.  
  243.  
  244.  
  245. <!-- STEP TWO: Insert the onLoad event handler into your BODY tag  -->
  246.  
  247.  
  248.  
  249. <BODY onload="cookieForms('open', 'yourform')" onunload=>
  250.  
  251.  
  252.  
  253. <!-- STEP THREE: Copy code into BODY section of document  -->
  254.  
  255.  
  256.  
  257. <body onload="cookieForms('open', 'yourform')" onunload="cookieForms('save', 'yourform')">
  258.  
  259. </p>
  260.  
  261. <hr size="1" width="300" align="left">
  262.  
  263. <form name="yourform">
  264.  
  265.  <p>Text Fields: 
  266.  
  267.   <input type="text" name="1" value="">
  268.  
  269.  </p>
  270.  
  271.  <p>Passwords: 
  272.  
  273.   <input type="password" name="2" value="">
  274.  
  275.   <br>
  276.  
  277.   (won't be saved)</p>
  278.  
  279.  <p>TextAreas: 
  280.  
  281.   <textarea name="3"></textarea>
  282.  
  283.  </p>
  284.  
  285.  <p>Dropdowns: 
  286.  
  287.   <select name="4">
  288.  
  289.    <option value="one">One</option>
  290.  
  291.    <option value="two">Two</option>
  292.  
  293.    <option value="three">Three</option>
  294.  
  295.   </select>
  296.  
  297.  </p>
  298.  
  299.  <p>Checkboxes: 
  300.  
  301.   <input type="checkbox" name="5" value="ummm">
  302.  
  303.  </p>
  304.  
  305.  <p>Radio Buttons: 
  306.  
  307.   <input type="radio" name="6" value="snuh">
  308.  
  309.   <input type="radio" name="6" value="whuf">
  310.  
  311.  </p>
  312.  
  313.  <hr size="1" width="300" align="left">
  314.  
  315. </form>
  316.  
  317. </BODY>
  318.  
  319. </HTML>
  320.  
  321.  
  322. <!-- END OF SCRIPT -->
  323. <!/SCRIPT>
  324.  
  325. <!PREVIEW>
  326. <!-- START OF SCRIPT -->
  327.  
  328.  
  329. <!-- HOW TO INSTALL COOKIE FORM SAVER:
  330.  
  331.  
  332.  
  333.   1.  Copy code into the HEAD section of document
  334.  
  335.   2.  Add the onLoad event handler into the BODY tag
  336.  
  337.   3.  Put last coding into the BODY section of document  -->
  338.  
  339.  
  340.  
  341. <!-- STEP ONE: Add code into HEAD section of document  -->
  342.  
  343.  
  344.  
  345. <HEAD>
  346.  
  347.  
  348.  
  349. <SCRIPT LANGUAGE="JavaScript">
  350.  
  351.  
  352. <!-- Original:  Nick Baker -->
  353.  
  354. <!-- Begin
  355.  
  356. // Cookie Functions  ////////////////////  (:)
  357.  
  358.  
  359.  
  360. // Set the cookie.
  361.  
  362. // SetCookie('your_cookie_name', 'your_cookie_value', exp);
  363.  
  364.  
  365.  
  366. // Get the cookie.
  367.  
  368. // var someVariable = GetCookie('your_cookie_name');
  369.  
  370.  
  371.  
  372. var expDays = 100;
  373.  
  374. var exp = new Date(); 
  375.  
  376. exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
  377.  
  378.  
  379.  
  380. function getCookieVal (offset) {  
  381.  
  382.     var endstr = document.cookie.indexOf (";", offset);  
  383.  
  384.     if (endstr == -1) { endstr = document.cookie.length; }
  385.  
  386.     return unescape(document.cookie.substring(offset, endstr));
  387.  
  388. }
  389.  
  390.  
  391.  
  392. function GetCookie (name) {  
  393.  
  394.     var arg = name + "=";  
  395.  
  396.     var alen = arg.length;  
  397.  
  398.     var clen = document.cookie.length;  
  399.  
  400.     var i = 0;  
  401.  
  402.     while (i < clen) {    
  403.  
  404.         var j = i + alen;    
  405.  
  406.         if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
  407.  
  408.         i = document.cookie.indexOf(" ", i) + 1;    
  409.  
  410.         if (i == 0) break;   
  411.  
  412.     }  
  413.  
  414.     return null;
  415.  
  416. }
  417.  
  418.  
  419.  
  420. function SetCookie (name, value) {  
  421.  
  422.     var argv = SetCookie.arguments;  
  423.  
  424.     var argc = SetCookie.arguments.length;  
  425.  
  426.     var expires = (argc > 2) ? argv[2] : null;  
  427.  
  428.     var path = (argc > 3) ? argv[3] : null;  
  429.  
  430.     var domain = (argc > 4) ? argv[4] : null;  
  431.  
  432.     var secure = (argc > 5) ? argv[5] : false;  
  433.  
  434.     document.cookie = name + "=" + escape (value) + 
  435.  
  436.     ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
  437.  
  438.     ((path == null) ? "" : ("; path=" + path)) +  
  439.  
  440.     ((domain == null) ? "" : ("; domain=" + domain)) +    
  441.  
  442.     ((secure == true) ? "; secure" : "");
  443.  
  444. }
  445.  
  446.  
  447.  
  448. // cookieForms saves form content of a page.
  449.  
  450.  
  451.  
  452. // use the following code to call it:
  453.  
  454. //  <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">
  455.  
  456.  
  457.  
  458. // It works on text fields and dropdowns in IE 5+
  459.  
  460. // It only works on text fields in Netscape 4.5
  461.  
  462.  
  463.  
  464.  
  465.  
  466. function cookieForms() {  
  467.  
  468.     var mode = cookieForms.arguments[0];
  469.  
  470.     
  471.  
  472.     for(f=1; f<cookieForms.arguments.length; f++) {
  473.  
  474.         formName = cookieForms.arguments[f];
  475.  
  476.         
  477.  
  478.         if(mode == 'open') {    
  479.  
  480.             cookieValue = GetCookie('saved_'+formName);
  481.  
  482.             if(cookieValue != null) {
  483.  
  484.                 var cookieArray = cookieValue.split('#cf#');
  485.  
  486.                 
  487.  
  488.                 if(cookieArray.length == document[formName].elements.length) {
  489.  
  490.                     for(i=0; i<document[formName].elements.length; i++) {
  491.  
  492.                     
  493.  
  494.                         if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
  495.  
  496.                         else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
  497.  
  498.                         else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
  499.  
  500.                         else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }
  501.  
  502.                     }
  503.  
  504.                 }
  505.  
  506.             }
  507.  
  508.         }
  509.  
  510.                 
  511.  
  512.         if(mode == 'save') {    
  513.  
  514.             cookieValue = '';
  515.  
  516.             for(i=0; i<document[formName].elements.length; i++) {
  517.  
  518.                 fieldType = document[formName].elements[i].type;
  519.  
  520.                 
  521.  
  522.                 if(fieldType == 'password') { passValue = ''; }
  523.  
  524.                 else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
  525.  
  526.                 else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
  527.  
  528.                 else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
  529.  
  530.                 else { passValue = document[formName].elements[i].value; }
  531.  
  532.             
  533.  
  534.                 cookieValue = cookieValue + passValue + '#cf#';
  535.  
  536.             }
  537.  
  538.             cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
  539.  
  540.             
  541.  
  542.             SetCookie('saved_'+formName, cookieValue, exp);        
  543.  
  544.         }    
  545.  
  546.     }
  547.  
  548. }
  549.  
  550. //  End -->
  551.  
  552. </script>
  553.  
  554.  
  555.  
  556. </HEAD>
  557.  
  558.  
  559.  
  560. <!-- STEP TWO: Insert the onLoad event handler into your BODY tag  -->
  561.  
  562.  
  563.  
  564. <BODY onload="cookieForms('open', 'yourform')" onunload=>
  565.  
  566.  
  567.  
  568. <!-- STEP THREE: Copy code into BODY section of document  -->
  569.  
  570.  
  571.  
  572. <body onload="cookieForms('open', 'yourform')" onunload="cookieForms('save', 'yourform')">
  573.  
  574. </p>
  575.  
  576. <hr size="1" width="300" align="left">
  577.  
  578. <form name="yourform">
  579.  
  580.  <p>Text Fields: 
  581.  
  582.   <input type="text" name="1" value="">
  583.  
  584.  </p>
  585.  
  586.  <p>Passwords: 
  587.  
  588.   <input type="password" name="2" value="">
  589.  
  590.   <br>
  591.  
  592.   (won't be saved)</p>
  593.  
  594.  <p>TextAreas: 
  595.  
  596.   <textarea name="3"></textarea>
  597.  
  598.  </p>
  599.  
  600.  <p>Dropdowns: 
  601.  
  602.   <select name="4">
  603.  
  604.    <option value="one">One</option>
  605.  
  606.    <option value="two">Two</option>
  607.  
  608.    <option value="three">Three</option>
  609.  
  610.   </select>
  611.  
  612.  </p>
  613.  
  614.  <p>Checkboxes: 
  615.  
  616.   <input type="checkbox" name="5" value="ummm">
  617.  
  618.  </p>
  619.  
  620.  <p>Radio Buttons: 
  621.  
  622.   <input type="radio" name="6" value="snuh">
  623.  
  624.   <input type="radio" name="6" value="whuf">
  625.  
  626.  </p>
  627.  
  628.  <hr size="1" width="300" align="left">
  629.  
  630. </form>
  631.  
  632. </BODY>
  633.  
  634. </HTML>
  635. <!-- END OF SCRIPT -->
  636. <!/PREVIEW>
  637.  
  638. <!RELATED>NONE<!/RELATED>
  639.